package cn.dhnep.demo.controller;


import cn.dhnep.demo.pojo.Homepageimg;
import cn.dhnep.demo.pojo.Horseman;
import cn.dhnep.demo.pojo.Order;
import cn.dhnep.demo.service.HorsemanService;
import cn.dhnep.demo.util.Page;
import cn.dhnep.demo.util.ResultMap;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dhn
 * @since 2021-08-24
 */
@RestController
@RequestMapping("/horseman")
public class HorsemanController {

    @Resource
    private HorsemanService horsemanService;

    private Map<String,Object> result = new HashMap<>();


    @ApiOperation("骑手分页查询")
    @RequestMapping("horsemanlistpage")
    public ResultMap<List<Horseman>> NoticeList(String horsemanPhone,String horsemanArea,String horsemanName,Page page, @RequestParam("limit") Integer limit) throws UnsupportedEncodingException {
        if(horsemanPhone!=null){
            page.setHorsemanPhone(horsemanPhone.trim());
        }
        if (horsemanName!=null){
            page.setHorsemanName(horsemanName.trim());
        }
        if (horsemanArea!=null){
            page.setHorsemanArea(horsemanArea.trim());
        }
        page.setRows(limit);
        List<Horseman>horsemanList=horsemanService.selectPageList(page);
        int totals=horsemanService.selectPageCount(page);
        page.setTotalRecord(totals);
        return new ResultMap<List<Horseman>>("",horsemanList,0,totals);
    }

    @ApiOperation("新增骑手")
    @RequestMapping("horsemanAdd")
    public Map<String,Object> horsemanAdd(@RequestBody Horseman horseman, HttpSession session, HttpServletRequest request, HttpServletResponse response) {
        int count =horsemanService.horsemanAdd(horseman);
        if (count==1){
            result.put("result","success");
        }else{
            result.put("result","error");
        }
        return result;
    }

    @ApiOperation("id查询骑手")
    @RequestMapping("idqueryhorseman")
    public void idqueryOrder(Integer horsemanId, HttpSession session, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println(horsemanId);
        Horseman horseman =new Horseman();
        horseman.setHorsemanId(horsemanId);
        horseman =horsemanService.idqueryhorseman(horseman);
        System.out.println(horseman);
        request.setAttribute("horseman",horseman);
        session.setAttribute("horseman",horseman);
        request.getRequestDispatcher( "../page/horseman-edit.html" ).forward(request,response);
    }

    @GetMapping("idquerysession")
    public Map<String,Object> idquerysession(HttpServletRequest request){
        HttpSession session = request.getSession();
        Horseman horseman =(Horseman)session.getAttribute("horseman");
        result.put("horseman",horseman);
        return result;
    }

    @ApiOperation("修改骑手信息")
    @RequestMapping("updateHorseman")
    public  Map<String,Object> updateHorseman(@RequestBody Horseman horseman, HttpSession session, HttpServletRequest request, HttpServletResponse response) {
        int count =horsemanService.horsemanUpda(horseman);
        if (count==1){
            result.put("result","success");
        }else{
            result.put("result","error");
        }
        return result;
    }

}

